<template>
  <div class="content">
    <MainPage :title="'芯片库'">
      <div class="mainContent">
        <div class="searchDiv">
          <el-input
            v-model="search.name"
            placeholder="请输入芯片名称或芯片型号"
            class="searchInput"
            @keyup.enter.native="searchClick"
          />
          <el-button type="primary" class="topBtn" @click="searchClick">查询</el-button>
          <el-button class="topBtn" @click="clearClick">重置</el-button>
          <el-button
            type="primary"
            style="position: absolute;right: 100px;"
            class="topBtn"
            @click="handleClick('circuitGenerationModel')"
          >
            导入或导出芯片名称统计表
          </el-button>
          <el-button
            v-permission="['chipLibraryManagement:chipLibrary:add']"
            style="position: absolute; right: 0"
            type="primary"
            class="topBtn"
            @click="handleClick('add')"
          >
            添加芯片
          </el-button>
        </div>
        <el-tabs v-model="activeName" class="tabsContent">
          <el-tab-pane label="主控芯片" name="masterChip">
            <chipList
              ref="chipList1"
              :chip-type="1"
              :category-list="category.masterChipCategory"
              :fixed-chip-category="category.fixedMasterChipCategory"
              @refresh="refresh"
              @handleClick="handleClick"
            />
          </el-tab-pane>
          <el-tab-pane label="被控芯片" name="slaveChip">
            <chipList
              ref="chipList2"
              :chip-type="2"
              :category-list="category.slaveChipCategory"
              :fixed-chip-category="category.fixedSlaveChipCategory"
              @refresh="refresh"
            />
          </el-tab-pane>
        </el-tabs>
      </div>
      <circuitGenerationChipModel ref="circuitGenerationChipModel" @refresh="refresh"/>
    </MainPage>
    <addChip ref="addChip" @refresh="refresh" />
    <updateChip ref="updateChip" @refresh="refresh" />
  </div>
</template>

<script>
import addChip from './components/addChip.vue'
import updateChip from './components/updateChip.vue'
import chipList from './components/chipList.vue'
import { loadChipCategoryList } from '@/api/project-management'
import defaultSettings from '@/settings'
import { generateUUID, postMessage, receivedMessage } from '@/utils/index'
import circuitGenerationChipModel from '@/views/project-list/components/circuitGenerationChipModel.vue'
export default {
  name: 'ChipLibrary',
  // eslint-disable-next-line vue/no-unused-components
  components: { chipList, addChip, updateChip, circuitGenerationChipModel},
  data() {
    return {
      pageType: 'index',
      search: { name: '' },
      activeName: 'masterChip',
      category: { masterChipCategory: [], slaveChipCategory: [] },
      loadChipCategoryListRequestId: generateUUID(),
      getChipListRequestId: generateUUID()
    }
  },
  created() {
    this.loadCategoryList()
  },
  mounted() {
    this.searchClick()
    receivedMessage(this.dataHandle)
  },
  methods: {
    dataHandle(e) {
      let self = this
      let result = defaultSettings.isWPF ? e : { data: JSON.parse(e) }
      if (result.data.webInfo.requestId === self.loadChipCategoryListRequestId) {
        self.category = result.data.response.data
      }
    },
    loadCategoryList() {
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/chip/loadChipCategoryList',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: {},
          WebInfo: {
            requestId: this.loadChipCategoryListRequestId
          }
        }
        postMessage(message)
      } else {
        loadChipCategoryList().then((res) => {
          this.category = res.data
        })
      }
    },
    refresh(chipType) {
      this.loadCategoryList()
      if (chipType === 1) {
        this.$refs.chipList1.handleSearch()
      } else {
        this.$refs.chipList2.handleSearch()
      }
    },
    searchClick() {
      this.$refs.chipList1.refreshData(this.search.name)
      this.$refs.chipList2.refreshData(this.search.name)
    },
    clearClick() {
      this.search.name = ''
      this.searchClick()
    },
    handleClick(name, row = {}) {
      switch (name) {
        case 'add':
          this.pageType = 'addChip'
          this.$refs.addChip.is(this.activeName === 'masterChip' ? 1 : 2)
          break
        case 'updateChip':
          this.pageType = 'updateChip'
          this.$refs.updateChip.is(row)
          break
        case 'circuitGenerationModel':
          this.$refs.circuitGenerationChipModel.is()
          break
      }
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  height: 100%;
  display: flex;
  flex-direction: column;
  .mainContent {
    flex: 1;
    height: calc(100% - 15px);
    .searchDiv {
      margin-bottom: 10px;
      position: relative;
      .searchInput {
        width: 300px;
        padding-right: 0;
        margin-right: 15px;
      }
    }
    .tabsContent {
      height: calc(100% - 42px);

      /deep/.el-tabs__content {
        height: calc(100% - 55px);
        .el-tab-pane {
          height: 100%;
        }
      }
    }
  }
}
</style>
